package com.baylor.server.business.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.WebUtils;

import com.baylor.server.base.util.WebUitl;
import com.baylor.server.business.entity.Admin;
import com.baylor.server.business.entity.UserInfo;
import com.baylor.server.business.service.UserInfoService;
import com.baylor.server.business.utils.FolderPath;
import com.baylor.server.business.utils.LangHelper;

/**
 * @ClassName: UserInfo1Controller
 * @Description: 这个类用于ajax模板加载测试
 * @author hchen
 * @date 2014年7月23日 下午3:56:10
 *
 */
@Controller
@RequestMapping("/user1")
public class UserInfo1Controller {
	@Autowired
	private UserInfoService userInfoService;
	
	/**
	 * 用户列表
	 */
	@ResponseBody
	@RequestMapping(value="userList")
	public Page<UserInfo> userList(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value="nextPage", defaultValue="0")Integer nextPage,
			@RequestParam(value="sortDate",defaultValue="")String sortDate,
			@RequestParam(value="jsonFilter",defaultValue="")String jsonFilter
			){
//		Pageable pageable = new PageRequest(nextPage,20,new Sort(new Order(Direction.DESC, "userRegister")));//
		Pageable pageable = WebUitl.buildPageRequest(nextPage, 10, sortDate);

		Map<String,Object> filterParams =  WebUitl.getParametersStartingWith(jsonFilter, "search_"); 
		filterParams.put("EQ_zhOren",LangHelper.getLang(request));
		Page<UserInfo> data = userInfoService.findUserDataByFilter(filterParams,pageable);
		
		return data;	
	}
	
	/**
	 * 详情 --页面
	 */
	@RequestMapping(value="userDetailByUserIdPage")
	public String userDetailByUserIdPage(HttpServletRequest request,@RequestParam(value="userId",defaultValue="")Integer userId){
		UserInfo user = userInfoService.findOne(userId);
		request.setAttribute("user", user);
		return "userDetail";
		
	}
	/**
	 * 新建 --页面
	 */
	@RequestMapping(value="userCreatePage")
	public String userCreatePage(){
		return "userCreate";
		
	}
	/**
	 * 新建 --数据库 保存
	 */
	@RequestMapping(value="userSave")
	public String userSave(HttpServletRequest request,@RequestParam("userPhoto2") MultipartFile file,UserInfo user){
		Admin admin =(Admin) request.getSession().getAttribute("admin");
		if(admin!=null)
		{
			if("en".equals(admin.getLang()))//英文
			{
				user.setZhOren(2);;
			}else if("cn".equals(admin.getLang()))//中文
			{
				user.setZhOren(1);
			} 
		}
		if(file!=null&&!file.isEmpty()){
			try {
				user.setUserPhoto(FolderPath.uploadFile(request, file, FolderPath.userphoto,".png"));
			} catch (IOException e) {}
		}
		user.setUserPhoto("img/head002.png");
		userInfoService.save(user);
		return "redirect:/user/userList";
		
	}
	
	/**
	 * 禁止登陆 --页面
	 */
	@RequestMapping(value="userForbid")
	public String userForbid(@RequestParam(value="userId",defaultValue="")Integer userId){
		userInfoService.userForbid(userId);
		return "redirect:/user/userList";
		
	}
	/**
	 * 允许登陆 --页面
	 */
	@RequestMapping(value="userAllow")
	public String userAllow(@RequestParam(value="userId",defaultValue="")Integer userId){
		userInfoService.userAllow(userId);
		return "redirect:/user/userList";
		
	}
	/**
	 * 修改 --页面
	 */
	@RequestMapping(value="userUpdateByUserIdPage")
	public String userUpdateByUserIdPage(HttpServletRequest request,@RequestParam(value="userId",defaultValue="")Integer userId){
		UserInfo user = userInfoService.findOne(userId);
		request.setAttribute("user", user);
		return "userUpdate";
		
	}
	/**
	 * 修改 --数据库保存
	 */
	@RequestMapping(value="userUpdateByUserIdSave")
	public String userUpdateByUserIdSave(HttpServletRequest request,UserInfo user,@RequestParam("userPhoto2") MultipartFile file){
		if(file!=null&&!file.isEmpty()){
			try {
				user.setUserPhoto(FolderPath.uploadFile(request, file, FolderPath.userphoto,".png"));
			} catch (IOException e) {}
		}
		//Admin admin =(Admin) request.getSession().getAttribute("admin");
		
		userInfoService.userUpdateByUserIdSave(user);
		
		return "redirect:/user/userList";
		
	}
	
	/**
	 * 初始化密码：手机号后6位
	 */
	
	@RequestMapping(value="userPwdChuShiHua")
	public String userPwdChuShiHua(HttpServletRequest request,@RequestParam(value="userId",defaultValue="")Integer userId,
			@RequestParam(value="pwd",defaultValue="")String pwd){
		if(!StringUtils.isNotBlank(pwd))
		{
			UserInfo user = userInfoService.userPwdChuShiHua(userId);
			if(user.getUserPassword().equals(user.getUserPhone().substring(user.getUserPhone().length()-6,user.getUserPhone().length()))){
				
				return "redirect:/user/userList?pwd=success";
			}
		} 
		return "redirect:/user/userList";
		
	}
	
	@ResponseBody
	@RequestMapping("delUserInfoByIds")
	public String delUserInfoByIds(HttpServletRequest request,@RequestParam(value="ids")String ids){
		String flag="";
		if(ids.endsWith(","))ids=ids.substring(0,ids.length()-1);
		String[] _ids=ids.split(",");
		try{
			for (int i = 0; i < _ids.length; i++) {			
				userInfoService.delUserInfo(Integer.parseInt(_ids[i]));
			}			
			flag="true";
		}catch(Exception e){
			flag="删除失败："+e.getMessage();
		}
		return flag;
	}
}
